package lib;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.*;
import java.io.*;

public class SearchSpLib {
	static String tbname;
	static Statement stat;
	static double PM;
	public static double[][] exper_peak_group;
	public static ArrayList<Integer> index_list;
	public static ArrayList<double[][]> dta_list;
	public static ArrayList<Double> score_list;
	public static ArrayList<String> note_group_list;
	public static String[][] note_group;
	public static void search(File exper_sp)throws Exception
	{
		String dbname="phospho";
		String tablename="splib3";
		double win_width=10;
		
		exper_peak_group=exper_sp_resolve(exper_sp);
	    linkDB(dbname,tablename);
	    searchLib(PM,win_width,exper_peak_group);
	    int counter=0;
	    note_group=new String[note_group_list.size()][5];
	    for(String iter_note:note_group_list)
	    {
	    	String[] temp_group=iter_note.split("\t");
	    	note_group[counter++]=temp_group;
	    }
		
	}
	private static void linkDB(String dbname,String tablename)throws Exception
	{
		Connection con;
		
		String url="jdbc:mysql://localhost:3306/"+dbname;
		Class.forName("com.mysql.jdbc.Driver");
		String userName="root";
		String password="123";
		tbname=tablename;
	    con=DriverManager.getConnection(url,userName,password);
		stat=con.createStatement();
	}
	private static double[][] get_dta_group(String dta_str)
	{
		double[][] theo_peak_group;
		String[] dta_line_splits=dta_str.split("#");
		theo_peak_group=new double[dta_line_splits.length][2];
		int counter=0;
		for(String iter_peak:dta_line_splits)
		{
			//System.out.println(iter_peak);
			String[] peak_splits=iter_peak.split("\\*");
			theo_peak_group[counter][0]=Double.parseDouble(peak_splits[0]);
			theo_peak_group[counter++][1]=Double.parseDouble(peak_splits[1]);
		}
		return theo_peak_group;
	}
	private static double countCorr(double[][] exper_peak_group,double[][] theo_peak_group)
	{
		
		return SpCorrCount.whole_spectrum(exper_peak_group, theo_peak_group);
	}
	private static void searchLib(double PM,double win_width,double[][] exper_peak_group)throws Exception
	{
		
		double[][] theor_peak_group;
		String quer_sql="select pm,charge,peptide,ipi,dta2 " +
				"from "+tbname+" where PM<"+(PM+win_width)+" and PM>"+(PM-win_width);
		ResultSet result_set=stat.executeQuery(quer_sql);
		note_group_list=new ArrayList<String>();
		score_list=new ArrayList<Double>();
		dta_list=new ArrayList<double[][]>();
		while(result_set.next())
		{
			double pm=result_set.getDouble(1);
			pm=Math.round(pm*10000)/10000.0;
			int charge=result_set.getInt(2);
			String peptide=result_set.getString(3);
			String ipi=result_set.getString(4);
			String dta_str=result_set.getString(5);
			
			
			theor_peak_group=get_dta_group(dta_str);
			double score=countCorr(exper_peak_group,theor_peak_group);
			score=Math.round(score*10000)/10000.0;
			score_list.add(score);
			String note_group=peptide+"\t"+score+"\t"+String.valueOf(pm)+"\t"+String.valueOf(charge)+"\t"+ipi;
			note_group_list.add(note_group);
			dta_list.add(theor_peak_group);
		}
		BubbleSort.sort(score_list, note_group_list);
		index_list=BubbleSort.index_list;
		score_list=BubbleSort.husbd_sort;
		
		note_group_list=BubbleSort.wife_sort;
	}
	public static double[][] exper_sp_resolve(File exper_sp_file)throws Exception
	{
		BufferedReader infile=new BufferedReader (new FileReader(exper_sp_file));
		String rline=new String();
		PM=Double.parseDouble(infile.readLine().split("\\s+")[0]);
		ArrayList<double[]> peak_list=new ArrayList<double[]>();
		double[][] exper_peak_group;
		double Max_intens=0;
		while((rline=infile.readLine())!=null)
		{
			String[] peak_splits=rline.split("\\s+");
			double[] peak_pair=new double[2];
			peak_pair[0]=Double.parseDouble(peak_splits[0]);
			peak_pair[1]=Double.parseDouble(peak_splits[1]);
			peak_list.add(peak_pair);
			if(peak_pair[1]>Max_intens)
			{
				Max_intens=peak_pair[1];
			}
		}
		infile.close();
		exper_peak_group=new double[peak_list.size()][2];
		int counter=0;
		for(double[] iter_peak:peak_list)
		{
			exper_peak_group[counter][0]=iter_peak[0];
			exper_peak_group[counter++][1]=Math.round((iter_peak[1]*100/Max_intens)*1000)/1000.0;
		}
		
		return exper_peak_group;
	}
	public static double[][] exper_sp_resolve2(File exper_sp_file)throws Exception
	{
		BufferedReader infile=new BufferedReader (new FileReader(exper_sp_file));
		String rline=new String();
		PM=Double.parseDouble(infile.readLine().split("\\s+")[0]);
		ArrayList<double[]> peak_list=new ArrayList<double[]>();
		double[][] exper_peak_group;
		double Max_intens=0;
		while((rline=infile.readLine())!=null)
		{
			String[] peak_splits=rline.split("\\s+");
			double[] peak_pair=new double[2];
			peak_pair[0]=Double.parseDouble(peak_splits[0]);
			peak_pair[1]=Double.parseDouble(peak_splits[1]);
			peak_list.add(peak_pair);
			if(peak_pair[1]>Max_intens)
			{
				Max_intens=peak_pair[1];
			}
		}
		infile.close();
		exper_peak_group=new double[peak_list.size()][2];
		int counter=0;
		for(double[] iter_peak:peak_list)
		{
			exper_peak_group[counter][0]=iter_peak[0];
			exper_peak_group[counter++][1]=Math.round((iter_peak[1]*100/Max_intens)*1000)/1000.0;
		}
		
		return exper_peak_group;
	}
	public static void main(String[] args)throws Exception
	{
		SearchSpLib testsearch=new SearchSpLib();
		//testsearch.search("E:/TestLib/test_pep_dta/insulin_5min_phos_03.14051.14051.2.dta");
		System.out.println(testsearch.dta_list.size());
		for(int i=0;i<10&&i<testsearch.dta_list.size();i++)
		{
//			for(String iter_note:testsearch.note_group_list.get(i))
//			{
//				System.out.print(iter_note+"\t");
//			}
			System.out.println(testsearch.score_list.get(i)+"\t");
		}
	}

}
